package com.IntelligenceInsight.Myth.controller;

import com.IntelligenceInsight.Myth.model.Algorithm;
import com.IntelligenceInsight.Myth.service.MLJobService;
import com.IntelligenceInsight.Myth.service.AlgorithmService;
import com.IntelligenceInsight.Myth.util.NoteRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.UUID;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;

@RestController
public class MLJobController {
    @Autowired
    private MLJobService mlJobService;
    @Autowired
    private AlgorithmService algorithmService;

    @RequestMapping(value = {"/getMLJobListAll.do"}, method = RequestMethod.POST)
    public NoteRequest MLJobListAll(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int count = mlJobService.MLJobListAllCount();
        int page_temp = page;
        int limit_temp = limit;
        if (count < page * limit) {
            limit = count - (page - 1) * limit;
        }
        page = (page_temp - 1) * limit_temp;
        NoteRequest res = mlJobService.MLJobListAll(page,limit);
        res.setCount(count);
        return res;
    }

    @RequestMapping(value = {"/getMLJobListByOwner.do"}, method = RequestMethod.POST)
    public NoteRequest MLJobListByOwner(HttpServletRequest request) {
        String owner = request.getParameter("owner");
        NoteRequest res = mlJobService.MLJobListByOwner(owner);
        return res;
    }

    @RequestMapping(value = {"/getMLJobListMine.do"}, method = RequestMethod.POST)
    public NoteRequest MLJobListMine(HttpServletRequest request) {
        String owner = request.getSession().getAttribute("username").toString();
        NoteRequest res = mlJobService.MLJobListByOwner(owner);
        return res;
    }


    @RequestMapping(value = {"/getMLJobByUuid.do"}, method = RequestMethod.POST)
    public NoteRequest MLJobByUuid(HttpServletRequest request) {
        String uuid = request.getParameter("uuid");
        NoteRequest res = mlJobService.MLJobByUuid(uuid);
        return res;
    }


    @RequestMapping(value = {"/getMLJobByName.do"}, method = RequestMethod.POST)
    public NoteRequest JobByName(HttpServletRequest request) {
        String name = request.getParameter("name");
        NoteRequest res = mlJobService.MLJobByName(name);
        return res;
    }


    @RequestMapping(value = {"/getMLJobByAlgorithm.do"}, method = RequestMethod.POST)
    public NoteRequest MLJobByAlgorithm(HttpServletRequest request) {
        String algorithm = request.getParameter("algorithm");
        NoteRequest res = mlJobService.MLJobByAlgorithm(algorithm);
        return res;
    }

    @RequestMapping(value = {"/mlJobAdd.do"}, method = RequestMethod.POST)
    public NoteRequest MLJobAdd(HttpServletRequest request) {
//        String uuid = UUID.randomUUID().toString();
//        String name = request.getParameter("name");
//        String algorithmUuid = request.getParameter("algorithm");
//        String owner = request.getSession().getAttribute("username").toString();
//
//        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
//        String date = df.format(new Date());
//        String resultPath = "/myth/results/" + name + "_" + date + ".txt";
//
//        NoteRequest res = new NoteRequest();
//
//        // Step 1: model must be exist
//        res = algorithmService.ModelByUuid(modelUuid);
//        if(res.getStatus() != 0) {
//            return res;
//        }
//        Algorithm model = (Algorithm) res.getData();
//        String modelString = model.getModelString();
//
//        // Step 2: Job name and uuid must unique
//        res = mlJobService.JobByName(name);
//        if(res.getStatus() == 0){
//            res.setStatus(1);
//            res.setMsg("Job name is exist");
//            res.setData(null);
//            return res;
//        }
//        // TODO: If uuid is exist, should be create a new uuid and check again until uuid is unique
//        res = mlJobService.JobByUuid(uuid);
//        if(res.getStatus() == 0){
//            res.setStatus(1);
//            res.setMsg("Job uuid is exist, this operation is reasonable");
//            res.setData(null);
//            return res;
//        }
//
//        // Step 3: persist job info into database
//        res = mlJobService.JobAdd(uuid, name, modelUuid, owner, filePath, resultPath);
//
//
//        // Step 4: Submit job to Computing background
//        // TODO: Every job create a thread is not good. Better way is just one thread check submit
//        // TODO: queue and submit job, in addition, another thread to listen job status change.
//        new Thread(()->jobMgrService.submitQueue(uuid, modelUuid, modelString, filePath, resultPath)).start();
//
//        return res;
        return null;
    }

    @RequestMapping(value = {"/mlJobRemoveByUuid.do"}, method = RequestMethod.POST)
    public NoteRequest MLJobRemoveByUuid(HttpServletRequest request) {
        NoteRequest res = new NoteRequest();
        String uuid = request.getParameter("uuid");
        res = mlJobService.MLJobDelete(uuid);
        return res;
    }
}
